package cn.com.hatech.blog.module.admin.article.controller;

import cn.com.hatech.blog.common.constant.Types;
import cn.com.hatech.blog.module.admin.article.service.IArticleService;
import cn.com.hatech.blog.module.admin.category.entity.Meta;
import cn.com.hatech.blog.module.admin.category.service.IMetaService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @projectName: hatech-blog
 * @package: cn.com.hatech.blog.module.admin.controller
 * @className: ArticleController
 * @author: xuruikai
 * @description: 文章管理页面跳转控制器
 * @date: 2020/1/11 14:27
 * @version: 1.0
 */
@Controller
@RequestMapping("/admin/article")
public class ArticlePageController {

    /**
     * 文章内容业务层接口注入
     */
    private final IArticleService contentsService;
    /**
     * 分类管理业务层接口注入
     */
    private final IMetaService metasService;

    @Autowired
    public ArticlePageController(IMetaService metasService, IArticleService contentsService) {
        this.metasService = metasService;
        this.contentsService = contentsService;
    }

    /**
     * 文章列表
     *
     * @param page
     * @param limit
     * @param request
     * @return
     */
    @GetMapping(value = "")
    public String index(HttpServletRequest request, @RequestParam(value = "page", defaultValue = "1") int page, @RequestParam(value = "limit", defaultValue = "10") int limit) {
        // 分页查询文章列表
        request.setAttribute("articles", contentsService.page(new Page<>(page,limit)));
        return "admin/article/article_list";
    }

    /**
     * 文章发表页面跳转入口
     *
     * @param request
     * @return
     */
    @GetMapping(value = "/publish")
    public String newArticle(HttpServletRequest request) {
        // 获取所有分类,供文章发布时选择
        List<Meta> metas = metasService.selectAllMetasByType(Types.CATEGORY.getType());
        request.setAttribute("categories", metas);
        request.setAttribute("categoryNames", metas.stream().map(Meta::getName).collect(Collectors.joining(",")));
        return "admin/article/article_edit";
    }

    /**
     * 文章编辑页面跳转入口
     *
     * @param id
     * @param request
     * @return
     */
    @GetMapping(value = "/{id}")
    public String editArticle(@PathVariable("id") String id, HttpServletRequest request) {
        // 查询需要编辑的文章内容
        request.setAttribute("content", contentsService.selectByIdWithoutHit(id));
        // 获取所有分类,供文章发布时选择
        List<Meta> metas = metasService.selectAllMetasByType(Types.CATEGORY.getType());
        request.setAttribute("categories", metas);
        request.setAttribute("categoryNames", metas.stream().map(Meta::getName).collect(Collectors.joining(",")));
        request.setAttribute("active", "article");
        return "admin/article/article_edit";
    }

}
